package main

import (
	"fmt"
	"time"
)

func main() {
	ch := make(chan string)	//创建管道
	go producer(ch)
	go consumer(ch)
	time.Sleep(5 * time.Second)
}

func producer(ch chan string) {
	for i := 0; i < 100; i++ {
		str := fmt.Sprintf("hello_%d", i)
		ch <- str	//数据写入管道
		fmt.Printf("produce data '%s' success\n", str)
	}
}

func consumer(ch chan string) {
	var data string	//声明字符串类型变量
	for {
		data = <- ch
		fmt.Println(data)
	}
}
